(function() {

	define(['jquery', 'cxslide'], function() {

		$(function() {

			$('.find-brand').cxSlide({//品牌轮播
				btn: false,
				speed: 600,
				plus: true,
				minus: true,
				auto: false
			});

			$('.area .inner .block').cxSlide({//区域轮播
				events: 'mouseover',
				type: 'fade',
				speed: 300,
				auto: false
			});

			var floor = $('body > .floor');//楼层
			var fLoadF = [];//已加载楼层
			var fFloorS = $('.floor').eq(0).offset().top;
			var fFloorL = $('.floor').last().offset().top;
			var left = $('.floor .inner').eq(0).offset().left;
			$('.floor-guide').css('left', left-40);

			$('.floor-guide .block').on('click', function() {

				$('html, body').animate({
					scrollTop: $('.floor.floor-' + $(this).attr('floor')).offset().top - 100
				}, 400);
			});

			var li = {
				'商品': '输入商品名称',
				'供货商': '输入供货商名称',
				'产地': '输入产地名称'
			}
			$('.i-fade-search .search-bar > .list ul li').on('click', function() {

				var text = $(this).html();
				if(text) {
					$(this).html($('.i-fade-search .search-bar > .list p.text').html());
					$('.i-fade-search .search-bar > .list p.text').html(text);
					if(window.applicationCache)
						$('.i-fade-search .search-bar .tt-input').attr('placeholder', li[text]);
					else
						$('.i-fade-search .search-bar .tt-input').attr('value', li[text]);
				}
			});
		
			$(window).on('scroll', function() {

				var scrollTop = $(window).scrollTop();
				if(scrollTop > 500) {//置顶搜索出现
					$('.to-top .one.top').fadeIn('fast');
					$('.b-search .search-bar input.tt-input').val('');
					$('.i-fade-search').fadeIn(150);
				}
				else {
					$('.to-top .one.top').fadeOut('fast');
					$('.i-fade-search .search-bar input.tt-input').val('');
					$('.i-fade-search').fadeOut(150);
				}
				
				if(scrollTop >= fFloorS - 100 && scrollTop <= fFloorL + 100)
					$('.floor-guide').fadeIn(100);
				else
					$('.floor-guide').fadeOut(100);

				floor.each(function(i, item) {

					if(fLoadF.indexOf($(item).attr('floor')) > -1) return;

					var res = inViewport(item);

					if(res)
						$.ajax({
							type: 'GET',
							url: '/esvml/web/index/floor.htm?floor=' + $(item).attr('floor'),
							dataType: 'html',
							success: function(data) {
								if(data)
									if($(item).attr('on') != 'true') {
										$(item).find('.bg-img').remove();
										$(item).find('div.inner').append(data);
										$(item).attr('on', 'true');
									}
								fLoadF.push($(item).attr('floor'))
							}
						});
				});
			}).trigger('scroll');

			function inViewport( el ) {
				var top = $(window).scrollTop(),
				btm = $(window).scrollTop() + $(window).height(),
           		elTop = $(el).offset().top;
            	return elTop >= top && elTop - 200 <= btm;
          	}
		});
	});
})();